<?php

namespace App\Http\Controllers;

use App\Model\Category;
use App\Models\Archive\Archive;
use App\Models\Live\Anchor;
use App\Models\Tag\Tag;
use Illuminate\Http\Request;

use App\Http\Requests;
use Illuminate\Support\Facades\Input;

class TagHeadController extends Controller
{
    //
    public function index(Request $request, Tag $tag, $type = '')
    {

        if ($tag->status == 4)
            return response()->view('errors.code', ['msg' => '该标签尚已停用！', 'code' => 302], 302);

        if ($tag->status != 2)
            return response()->view('errors.code', ['msg' => '该标签尚未通过审核！', 'code' => 302], 302);


        if ($tag->type == 'anchor') {
            //主播标签
            return (new AnchorController)->show($request, $tag);
        }elseif($tag->type == 'live'){

        }elseif(isset($tag->logo)&&isset($tag->background_image)) {
            //推荐标签

            $view = view('home.tag.tag_recommend');
        }else{
            //普通标签
            $view = view('home.tag.tag_normal');
        }


        $similars = $tag->getSimilars()->sortBy('r_weight')->values();

        $view->with('similars', $similars);

        $type = $type ? $type : $request->input('type');

        if($type == 'article'){
            $archives = $tag->archives()->where(function($query){
                $query->where('archive_type_id',1)
                      ->orWhere('archive_type_id',4);
            })->ofPattern('review')->paginate(8);
        }elseif($type == 'gallery'){
            $archives = $tag->archives()->where('archive_type_id',2)->ofPattern('review')->paginate(8);
        }elseif($type == 'video') {
            $archives = $tag->archives()->where('archive_type_id', 3)->ofPattern('review')->paginate(8);
        }else{
            $archives = $tag->archives()->ofPattern('review')->paginate(8);
        }
        $recommend = Tag::where('logo','<>',null)->where('background_image','<>',null)->take(3)->get();

        return $view
            ->with('archives', $archives->appends($request->only(['type'])))
            ->with('tag', $tag)
            ->with('recommend', $recommend)
            ->with('type', $type)
        ;
    }
}
